package bookManage.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

import Util.TestUtil;
import bookManage.model.BookModel;
import bookManage.model.BorrowModel;

public class BorrowUtil {

	/**
	 * 存放借阅记录
	 */
	public static ArrayList<BorrowModel> borrows = new ArrayList<BorrowModel>();

	static Scanner sc = new Scanner(System.in);

	static String path = TestUtil.creatPath("BookManage", "borrows");

	/**
	 * 查询全部借阅记录
	 */
	public void selectAll() {
		for (BorrowModel borrowModel : borrows) {
			info(borrowModel);
		}
	}

	/**
	 * 借阅记录详情
	 * 
	 * @param borrowModel
	 */
	public void info(BorrowModel borrowModel) {
		String name = searchBoName(borrowModel.getBookIsbn());
		System.out.println("-----------------------");
		System.out.println("借阅编号：" + borrowModel.getBorrowCode());
		System.out.println("借阅卡：" + borrowModel.getCardId());
		System.out.println("图书名：" + name);
		System.out.println("借阅时间：" + borrowModel.getBorrowTime());
		System.out.println("归还时间：" + borrowModel.getReturnTime());
		System.out.println("-----------------------");
	}

	/**
	 * 根据图书isbn查找图书名
	 * 
	 * @param isbn
	 * @return
	 */
	public String searchBoName(String isbn) {
		for (BookModel book : BookUtil.books) {
			if (book.getIsbn().equals(isbn)) {
				return book.getName();
			}
		}
		return null;
	}

	/**
	 * 根据图书名查找借阅信息
	 */
	public void selectByName() {
		System.out.print("请输入您要查找的图书名：");
		String name = sc.nextLine();

		for (BookModel book : BookUtil.books) {
			if (book.getName().equals(name)) {
				selectByIsbn(book.getIsbn());
			}
		}
	}

	/**
	 * 根据isbn查找借阅信息
	 * 
	 * @param isbn
	 * @param name
	 */
	public void selectByIsbn(String isbn) {
		for (BorrowModel borrowModel : borrows) {
			if (borrowModel.getBookIsbn().equals(isbn)) {
				info(borrowModel);
			}
		}
	}

	/**
	 * 查询个人借阅记录
	 */
	public void selectByUser() {
		for (BorrowModel borrowModel : borrows) {
			if (borrowModel.getCardId().equals(CardUtil.user)) {
				info(borrowModel);
			}
		}
	}

	/**
	 * 根据借阅卡查询借阅信息
	 */
	public void selectByCard() {
		System.out.println("请输入借阅卡号：");
		String cardId = sc.nextLine();
		for (BorrowModel borrowModel : borrows) {
			if (cardId.equals(borrowModel.getCardId())) {
				info(borrowModel);
			}
		}
	}

	static {
		File f = new File(path);
		if (f.length() != 0)
			borrows = TestUtil.read(path);
	}
}
